﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Uinside.CreditSystem.DomainModels.Models
{
    public partial class Order
    {
        public Order()
        {
            ReturnDate = DateTime.Now;
            ReturnInterestDate = DateTime.Now;
            this.PropertyChanged += OnPropertyChanged;
        }

        public bool IsMonthlyInterestEnabled
        {
            get { return this.BorrowedYear > 0 || this.BorrowedMonth > 0; }
        }

        public bool IsYearlyInterestEnabled
        {
            get { return this.BorrowedYear > 0; }
        }

        public bool IsDaylyInterestEnabled
        {
            get { return this.BorrowedYear > 0 || this.BorrowedMonth > 0 || this.BorrowedDay > 0; }
        }

        public double CalculateDayRateOneTime(double rate, DateTime from, DateTime to)
        {
            var days = (from - to).Days;
            if (days <= 0)
                return 0;
            return rate / days;
        }

        public double CalculateDayRate(double rate, int dateType)
        {
            switch (dateType)
            {
                //day
                case 0:
                    return rate;
                //month
                case 1:
                    return rate * 12 / 365;
                case 2:
                    return rate / 365;
            }
            throw new Exception("Issue interest type");
        }


        void CalculateDaylyRate()
        {
            if (this.InterestType == 0)
            {
                InterestRatePerDay = CalculateDayRateOneTime(InterestRate, this.ReturnDate, this.CreateDate);
                return;
            }
            //monthly
            InterestRatePerDay = CalculateDayRate(InterestRate, this.InterestDateType);
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (propertyChangedEventArgs.PropertyName == "InterestRate")
            {
                CalculateDaylyRate();
                return;
            }

            if (propertyChangedEventArgs.PropertyName == "InterestDateType")
            {
                CalculateDaylyRate();
                return;
            }

            if (propertyChangedEventArgs.PropertyName == "InterestType")
            {
                CalculateDaylyRate();
                if (this.InterestType == 0)
                {
                    InterestDateType = 0;
                    return;
                }
                //monthly

                return;
            }

            if (propertyChangedEventArgs.PropertyName == "ReturnInterestType")
            {
                if (this.ReturnInterestType == 0)
                {
                    ReturnInterestDateType = 0;
                }
                return;
            }

            if (propertyChangedEventArgs.PropertyName == "ReturnDate")
            {
                CalculateDaylyRate();
                return;
            }



            if (propertyChangedEventArgs.PropertyName == "BorrowedYear" ||
                propertyChangedEventArgs.PropertyName == "BorrowedMonth" ||
                propertyChangedEventArgs.PropertyName == "BorrowedDay")
            {
                this.ReturnDate =
                    this.CreateDate.AddYears(this.BorrowedYear).AddMonths(this.BorrowedMonth).AddDays(BorrowedDay);
                this.ReturnInterestDate = this.ReturnDate;
                OnPropertyChanged("IsMonthlyInterestEnabled");
                OnPropertyChanged("IsYearlyInterestEnabled");
                OnPropertyChanged("IsDaylyInterestEnabled");
            }


        }
    }
}
